home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / magict_1 / form1.frm next >
Text File  |  1999-07-22  |  11KB  |  407 lines

  1. VERSION 5.00
  2. Object = "{3035B5D2-295D-11D3-8C54-006008BA8D16}#1.0#0"; "MAGICTCP.OCX"
  3. Begin VB.Form Form1 
  4.    Caption         =   "Form1"
  5.    ClientHeight    =   9360
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   13830
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   9360
  11.    ScaleWidth      =   13830
  12.    StartUpPosition =   3  'Windows-Standard
  13.    WindowState     =   2  'Maximiert
  14.    Begin VB.CommandButton txtEnd 
  15.       Caption         =   "End"
  16.       Height          =   495
  17.       Left            =   1680
  18.       TabIndex        =   2
  19.       Top             =   120
  20.       Width           =   1335
  21.    End
  22.    Begin VB.CommandButton cmdClear 
  23.       Caption         =   "Clear"
  24.       Height          =   495
  25.       Left            =   240
  26.       TabIndex        =   1
  27.       Top             =   120
  28.       Width           =   1335
  29.    End
  30.    Begin VB.TextBox txtInfo 
  31.       BeginProperty Font 
  32.          Name            =   "Courier New"
  33.          Size            =   8.25
  34.          Charset         =   0
  35.          Weight          =   400
  36.          Underline       =   0   'False
  37.          Italic          =   0   'False
  38.          Strikethrough   =   0   'False
  39.       EndProperty
  40.       Height          =   8595
  41.       Left            =   240
  42.       MultiLine       =   -1  'True
  43.       ScrollBars      =   3  'Beides
  44.       TabIndex        =   0
  45.       Top             =   720
  46.       Width           =   11415
  47.    End
  48.    Begin M3LibCtl.MagicTCP Magic 
  49.       Left            =   3960
  50.       OleObjectBlob   =   "Form1.frx":0000
  51.       Top             =   120
  52.    End
  53.    Begin VB.Label Label1 
  54.       Alignment       =   1  'Rechts
  55.       Caption         =   "Copyright 1999, hiNRGware, All rights reserved!"
  56.       BeginProperty Font 
  57.          Name            =   "MS Sans Serif"
  58.          Size            =   8.25
  59.          Charset         =   0
  60.          Weight          =   700
  61.          Underline       =   0   'False
  62.          Italic          =   0   'False
  63.          Strikethrough   =   0   'False
  64.       EndProperty
  65.       Height          =   375
  66.       Left            =   6840
  67.       TabIndex        =   3
  68.       Top             =   240
  69.       Width           =   4575
  70.    End
  71. End
  72. Attribute VB_Name = "Form1"
  73. Attribute VB_GlobalNameSpace = False
  74. Attribute VB_Creatable = False
  75. Attribute VB_PredeclaredId = True
  76. Attribute VB_Exposed = False
  77. Option Explicit
  78.  
  79. Const CONN_CLIENT = 1   ' Connection from client (web browser)
  80. Const CONN_SERVER = 2   ' connection to web server / proxy server
  81.  
  82. ' Shall we connect indirectly via a proxy server (cascaded proxies)?
  83. Dim ProxyServer As String
  84. Dim ProxyPort As Long
  85.  
  86.  
  87. Private Sub Cleanup()
  88.  
  89. With Magic
  90.     .Delete .CurrentSocket
  91. End With
  92.  
  93. End Sub
  94.  
  95.  
  96. Private Sub Log(Evt As String)
  97.  
  98. Dim i As Integer
  99. Dim s As String
  100.  
  101. s = txtInfo & "[" & Magic.CurrentSocket & "]" & Evt & vbCrLf
  102.  
  103. While Len(s) > 32000
  104.     i = InStr(s, vbCrLf)
  105.     If (i > 0) Then
  106.         s = Mid(s, i + Len(vbCrLf))
  107.     Else
  108.         s = ""
  109.     End If
  110. Wend
  111.  
  112.  
  113. txtInfo = s
  114. txtInfo.SelStart = Len(s) + 1
  115.  
  116. End Sub
  117.  
  118.  
  119. Private Sub cmdClear_Click()
  120.  
  121. txtInfo = ""
  122.  
  123. End Sub
  124.  
  125. Private Sub Form_Load()
  126.  
  127. Dim tf As Boolean 'tf means True/False
  128.  
  129. With Magic
  130.     .LogEnable = True
  131.     .LogFile = "C:\TEMP\MAGICTCP.TXT"
  132.     
  133.     'Socket 0 is created automatically.
  134.     ' So we cab start right away by setting up the listening socket :)
  135.     
  136.     ' Check if a proxy is specfied, where this proxy should connect to (casciding)
  137.     ProxyServer = .GetProfileString("PROXY", "PROXYSERVER", "")
  138.     ProxyPort = .GetProfileInt("PROXY", "PROXYPORT", 0)
  139.     
  140.     ' Listen on port 8080 (by defualt) for incoming connection requests
  141.     .LocalPort = .GetProfileInt("PROXY", "PORT", 8080)
  142.     .ReUseAddr = True
  143.     
  144.     ' Let's listen
  145.     tf = .Listen
  146.     ' Something went wrong?
  147.     If Not tf Then
  148.         MsgBox "Listen_Error: " & .LastErrorText
  149.     End If
  150. End With
  151.  
  152. End Sub
  153.  
  154.  
  155. Private Sub Magic_OnAccept(ByVal ListenSocket As Long)
  156.  
  157. With Magic
  158.     ' new connection accepted. don't do anything here. Wait for data to arrive! See OnRead()
  159.     .zzType = CONN_CLIENT
  160.     
  161.     ' no data from client read so far
  162.     .zzBuffer = ""
  163.     
  164.     .LogSocket = True
  165. End With
  166.  
  167. End Sub
  168.  
  169. Private Sub Magic_OnClose()
  170.  
  171. With Magic
  172.     ' delete current socket and it's partner socket
  173.     .Delete .CurrentSocket
  174. End With
  175.  
  176. End Sub
  177.  
  178.  
  179. Private Sub Magic_OnConnect()
  180.  
  181. ' successfull connected. wait for the OnWrite() event to write data to distant host.
  182.  
  183. End Sub
  184.  
  185. Private Sub Magic_OnError(ByVal WinsockError As Long, ByVal Func As String)
  186.  
  187. With Magic
  188.     ' Don't care for asyncronous errors
  189.     '
  190.     ' delete current socket and it's partner socket
  191.     .Delete .CurrentSocket
  192. End With
  193.  
  194. End Sub
  195.  
  196. Private Sub Magic_OnRead()
  197.  
  198. ' data from client has arrived!
  199. '
  200. ' the web browser send an http requests, it's first line has the following structure:
  201. ' OPERATION http://hostname.anywhere.com/path/file?param=dontcare HTTP/1.x <cr> <lf>
  202. '
  203. ' we'll connect to specified host, and route the rest of the request and the full response
  204.  
  205.  
  206. Dim tf As Boolean
  207. Dim take As Boolean
  208. Dim s As String
  209. Dim i As Integer
  210. Dim currSock As Long
  211. Dim newSock As Long
  212. Dim hostname As String
  213. Dim hostport As Long
  214.  
  215.  
  216.  
  217. With Magic
  218.     tf = True                       ' no error so far
  219.     take = False                    ' first line is not complete yet
  220.     newSock = -1                    ' no new socket created
  221.     currSock = .CurrentSocket       ' remember number of current socket
  222.     
  223.     If tf Then
  224.         ' try to read the first line
  225.         If Not .IsValidSocket(currSock) Then
  226.             MsgBox "Error!!!!!!"
  227.         End If
  228.         
  229.         If Not .ReadString(s) Then
  230.             tf = False
  231.             Log "Read-Error: " & .LastErrorText
  232.         End If
  233.     End If
  234.     
  235.     If tf Then
  236.         ' try to isolate the first line which is terminated by <CR> <LF>
  237.         s = .zzBuffer & s
  238.         i = InStr(s, vbCrLf)
  239.         If i > 0 Then
  240.             take = True                     ' first line is complete
  241.             hostname = Left$(s, i - 1)
  242.         Else
  243.             ' first line is not complete. wait for the the next read event. data already received
  244.             ' is stored in the socket's zzBuffer
  245.             .zzBuffer = s
  246.         End If
  247.     End If
  248.     
  249.     If (tf And take) Then
  250.         ' let's log the request on the user interface
  251.         Log .RemoteHost & ":" & .RemotePort & " " & hostname
  252.         
  253.         ' no proxy server for our operation definied?
  254.         If (Len(ProxyServer) = 0) Then
  255.             ' extract the hostname (and port) from the HTTP-request
  256.             
  257.             i = InStr(1, hostname, " ")
  258.             If (i > 0) Then
  259.                 hostname = LTrim$(Mid$(hostname, i + 1))
  260.                 i = InStr(1, hostname, "//")
  261.                 
  262.                 If (i > 0) Then
  263.                     hostname = LTrim$(Mid$(hostname, i + 2))
  264.                     i = InStr(1, hostname, "/")
  265.                 End If
  266.                 
  267.                 If (i = 0) Then
  268.                     i = InStr(1, hostname, " ")
  269.                 End If
  270.             End If
  271.             
  272.             
  273.             If (i > 0) Then
  274.                 hostname = Left$(hostname, i - 1)
  275.                 i = InStr(1, hostname, ":")
  276.                 If (i = 0) Then
  277.                     hostport = 80
  278.                 Else
  279.                     hostport = CLng(Mid$(hostname, i + 1))
  280.                     hostname = Left$(hostname, i - 1)
  281.                 End If
  282.             Else
  283.                 tf = False
  284.             End If
  285.         Else
  286.             hostname = ProxyServer
  287.             hostport = ProxyPort
  288.         End If
  289.     End If
  290.     
  291.     If (tf And take) Then
  292.         ' do not receive any more data until connected from client
  293.         .OnReadEvent = False
  294.         
  295.         ' create a new socket and define this socket as our partner socket
  296.         newSock = .New
  297.         .PartnerSocket = newSock
  298.         
  299.         ' switch to the new socket and set it's partner socket as well
  300.         .Curr